
package model;

import java.util.ArrayList;
import model.entities.//nameClass//;
import model.dao.//nameClass//DAO;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

public class //nameClass//Model implements //nameClass//DAO  {

    private static SessionFactory factory;

    public //nameClass//Model() {
        try {
            factory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    @Override
    public boolean create(//nameClass// //nameclass//) {
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.saveOrUpdate(//nameclass//);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.err.println(e.getMessages());
            return false;
        } finally {
            session.close();
        }
        return true;
    }
    
    @Override
    public //nameClass// read(int id) {
        //nameClass// //nameclass// = null;   
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            //nameclass// = (//nameClass//) session.get(//nameClass//.class, id);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.err.println(e.getMessages());
        } finally {
            session.close();
        }
        return //nameclass//;
    }
    
    @Override
    public boolean update(//nameClass// //nameclass//) {
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.merge(//nameclass//);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.err.println(e.getMessages());
            return false;
        } finally {
            session.close();
        }
        return true;
    }
    
    @Override
    public boolean delete(int id) {
        Session session = factory.openSession();
        Transaction tx = null;
        //nameClass// //nameclass// = null;
        try {
            tx = session.beginTransaction();
            //nameclass// = (//nameClass//) session.get(//nameClass//.class, id);
            session.delete(//nameclass//);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.err.println(e.getMessages());
            return false;
        } finally {
            session.close();
        }
        return true;
    }    
    
    @Override
    public ArrayList<//nameClass//> list() {
        Session session = factory.openSession();
        Transaction tx = null;
        ArrayList<//nameClass//> list = null;
        try {
            tx = session.beginTransaction();
            list = (ArrayList<//nameClass//>) session.createQuery("from //nameClass//").list();
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.err.println(e.getMessages());
        } finally {
            session.close();
        }
        return list;
    }
    
   @Override
    public int count() {
        Session session = factory.openSession();
        Transaction tx = null;
        int count = 0;
        try {
            tx = session.beginTransaction();
            count = ((Long) session.createQuery("select count(*) from //nameClass//").uniqueResult()).intValue();
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.err.println(e.getMessages());
        } finally {
            session.close();
        }
        return count;
    }   

    @Override
    public int count(String sql) {
        Session session = factory.openSession();
        Transaction tx = null;
        int count = 0;
        try {
            tx = session.beginTransaction();
            Criteria crit = session.createCriteria(//nameClass//.class);
            crit.add(Restrictions.like("name","%"+sql+"%"));
            crit.setProjection(Projections.rowCount());
            count = (Integer)crit.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.err.println(e.getMessages());
        } finally {
            session.close();
        }
        return count;
    }

    @Override
    public ArrayList<//nameClass//> getList(int page_size, int page_number) {
        Session session = factory.openSession();
        Transaction tx = null;
        ArrayList<//nameClass//> //nameclass//s = null;
        try {
            tx = session.beginTransaction();
            Query query = session.createQuery("from //nameClass//");
            query.setFirstResult((page_number - 1) * page_size);
            query.setMaxResults(page_size);
            //nameclass//s = (ArrayList<//nameClass//>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.err.println(e.getMessages());
        } finally {
            session.close();
        }
        return //nameclass//s;
    }
    
    @Override
    public ArrayList<//nameClass//> all() {
        Session session = factory.openSession();
        Transaction tx = null;
        ArrayList<//nameClass//> //nameclass//s = null;
        try {
            tx = session.beginTransaction();
            Query query = session.createQuery("from //nameClass//");            
            //nameclass//s = (ArrayList<//nameClass//>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.err.println(e.getMessages());
        } finally {
            session.close();
        }
        return //nameclass//s;
    }
    
    @Override
    public ArrayList<//nameClass//> query(String sql,int page_size, int page_number) {
        Session session = factory.openSession();
        Transaction tx = null;
        ArrayList<//nameClass//> //nameclass//s = null;
        try {
            tx = session.beginTransaction();
            Criteria crit = session.createCriteria(//nameClass//.class);           
            crit.add(Restrictions.like("name","%"+sql+"%"));
            crit.setFirstResult((page_number - 1) * page_size);
            crit.setMaxResults(page_size);
            //nameclass//s = (ArrayList<//nameClass//>) crit.list();
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.err.println(e.getMessages());
        } finally {
            session.close();
        }
        return //nameclass//s;
    }
}
